/*
 * get.c
 *
 *  Created on: Sep 21, 2013
 *      Author: Joseph DeLong
 */

#include <stdio.h>

/*
 * GET function
 * Written by Joseph DeLong
 * Borrowed heavily from Alex Bouvy's COMMHAND code to get input capturing working (in theory)
 * Modified to include dynamic allocation of memory for all captured string tokens (up to max of 256 tokens)
 * parameters: NONE
 * returns: A char ** containing the results of the text-fetching procedure
 */
char **get(void) {
	char **returnVal = NULL;
	int bufferSize = 256; //absolute maximum number of characters typed into the terminal at once
	char input[256]; //pre-processing storage for user input
	int stringSize; //holder for return value of sysreq() calls, use for error checking

	stringSize = sys_req(1, 1, input, &bufferSize);

	//INSERT ERROR CHECKING HERE!

	int i, j; //iterators used in for() loops
	int numTokens = 0; //number of captured tokens from terminal
	int tokenSize = 0; //temp storage for length of each token as it is being processed
	for(i = 0; i < stringSize; i++) { //look through captured input, character by character
		if(input[i] == ' ') { //if a whiteSpace char is found
			numTokens++; //increment number of tokens
		}
	}
	int tokenSizes[numTokens]; //array of token size information
	int count = 0; //counter for position in char array
	for(j = 0; j < numTokens; j++) { //iterate through number of tokens
		tokenSize = 0; //reset size of token
		for(i = count; i < stringSize; i++) { //iterate through string, always resuming from where we left off
			if(input[i] != ' ') { //if NOT a whitespace
				tokenSize++; //size of this token is still growing
				count++; //increment character position counter
			}
			else { //whitespace found
				tokenSizes[j] = tokenSize; //put size of token into array
				count++; //increment character position counter
				break; //move onto the next token
			}
		}
	}
	char ** inputTokens; //storage space for captured tokens
	inputTokens = sys_alloc_mem(sizeof(char *) * numTokens); //allocate memory for char pointers
	for(i = 0; i < numTokens; i++) { //iterate through tokens
		inputTokens[i] = sys_alloc_mem(sizeof(char) * tokenSizes[i]); //allocate memory for each token
	}
	inputTokens[0] = strtok(input," "); //this initial call to strtok() captures the first token only
	if(strcmp(inputTokens[0],"\n") == 0) { //if only a newLine character was captured, treat it as no input
		continue;
	}
	else { //otherwise, valid input was captured
		for(i = 1; i < numTokens; i++) { //start at position 1 and continue until all tokens are processed
			inputTokens[i] = strtok(NULL," "); //these subsequent calls to strtok() get additional tokens from the string, up to the value of numTokens
			if(inputTokens[i] == NULL) { //if what was captured was bogus token, quit looking for more
				break;
			}
			else { //otherwise, make sure number of tokens captured is still accurate
				numTokens = i;
			}
		}
	}
	//hacky fix for tokenization
	if(numTokens == 0) {
		strxfrm(inputTokens[0], inputTokens[0], strlen(inputTokens[0])); //WHAT DOES THIS ACTUALLY DO??? SHOULD I ADD [+ 1] TO THIS CALL?
		strcat(inputTokens[0], "\0");
	}
	//hacky space for other tokenization issue
	if(numTokens == 1) {
		if(strcmp(inputTokens[1], "\n") == 0) {
			numTokens = 0;
			inputTokens[1] = NULL;
		}
	}
	returnVal = inputTokens;//assign tokens to be returned
	return returnVal;
}
